﻿using MediatR;
using W.Warehouse.Api.Application.Command.CheckCommand;
using W.Warehouse.Repository.Impl;
using W.Warehouse.Repository.Interface;

namespace W.Warehouse.Api.Application.CommandHandlder.CheckCommandHandler
{
    public class CheckDeleteCommandHandler:IRequestHandler<CheckDeleteCommand,int>
    {
        private readonly ICheckRepository _checkRepository;
        private readonly IStatisticsRepository _statisticsRepository;
        public CheckDeleteCommandHandler(ICheckRepository checkRepository, IStatisticsRepository statisticsRepository)
        {
            _checkRepository = checkRepository;
            _statisticsRepository = statisticsRepository;
        }

        public async Task<int> Handle(CheckDeleteCommand request, CancellationToken cancellationToken)
        {
            var clist = await _checkRepository.GetAllAsync();
            var slist = await _statisticsRepository.GetAllAsync();
            var list = (from a in clist
                        //join b in slist
                        //on a.CheckId equals b.CheckId
                        where a.CheckId == request.CheckId && a.IsDelete == false
                        select a).FirstOrDefault();
            if (list != null)
            {
                list.IsDelete = true;
            }
            return await _checkRepository.UpdateAsync(list);
        }
    }
}
